package cn.com.xy.sms.sdk.ui.popu.part;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xy.smartsms.facade.SmsProcessFacade;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.com.xy.sms.sdk.Iservice.XyCallBack;
import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.smsmessage.BusinessSmsMessage;
import cn.com.xy.sms.sdk.ui.R;
import cn.com.xy.sms.sdk.ui.popu.util.BottomButtonUtil;
import cn.com.xy.sms.sdk.ui.popu.util.ChannelContentUtil;
import cn.com.xy.sms.sdk.ui.popu.util.ContentUtil;
import cn.com.xy.sms.sdk.ui.popu.util.ContentUtil.OnBottomClick;
import cn.com.xy.sms.sdk.ui.popu.util.SimpleButtonUtil;
import cn.com.xy.sms.sdk.ui.popu.util.SmartSmsSdkUtil;
import cn.com.xy.sms.sdk.ui.popu.util.ThemeUtil;
import cn.com.xy.sms.sdk.ui.popu.util.ViewUtil;
import cn.com.xy.sms.sdk.ui.popu.widget.MoreButtonDataDialog;
import cn.com.xy.sms.sdk.util.JsonUtil;
import cn.com.xy.sms.sdk.util.StringUtils;

public class BubbleBottomTwoCMCC extends UIPart {

    private boolean mDisLogo = false;
    private View mBtn1 = null, mBtn2 = null, mBtn3 = null;
    private TextView mTextView1 = null, mTextView2 = null, mTextView3 = null;
    public int[] mHarr = new int[2];
    private ViewGroup mdlayout;
    private ViewGroup mBtnLay;

    public int mSize = 0;
    private static final int DRAWABLE_BOUNDS_TOP = (int) ViewUtil.getDimension(R.dimen.duoqu_drawable_bounds_top);
    private static final int DRAWABLE_BOUNDS_RIGHT = (int) ViewUtil.getDimension(R.dimen.duoqu_drawable_bounds_right);
    private static final int DRAWABLE_BOUNDS_BOTTOM = (int) ViewUtil.getDimension(R.dimen.duoqu_drawable_bounds_bottom);

    public BubbleBottomTwoCMCC(Activity mContext, BusinessSmsMessage message, XyCallBack callback, int layoutId,
                               ViewGroup root, int partId) {
        super(mContext, message, callback, layoutId, root, partId);
    }

    @Override
    public void initUi() throws Exception {
        mHarr[0] = Math.round(mContext.getResources().getDimension(R.dimen.bubble_bottom_two_height));
        mHarr[1] = Math.round(mContext.getResources().getDimension(R.dimen.bubble_bottom_two_minheight));
        mdlayout = (ViewGroup) mView.findViewById(R.id.duoqu_bubble_bottom_two);
        mBtnLay = (ViewGroup) mView.findViewById(R.id.duoqu_btn_layout) ;
        mBtn1 = mView.findViewById(R.id.duoqu_btn_1);
        mBtn2 = mView.findViewById(R.id.duoqu_btn_2);
        mBtn3 = mView.findViewById(R.id.duoqu_btn_3);
        mTextView1 = (TextView) mView.findViewById(R.id.duoqu_btn_text_1);
        mTextView2 = (TextView) mView.findViewById(R.id.duoqu_btn_text_2);
        mTextView3 = (TextView) mView.findViewById(R.id.duoqu_btn_text_3);

    }

    public void setButtonTextAndImg(TextView buttonText, String action, boolean disLogo) {
        try {
            String buttonName = buttonText.getText().toString();
            boolean setText = StringUtils.isNull(buttonName);

            int resLogoId = SimpleButtonUtil.bindButtonData(buttonText, action, setText, true);

            if (disLogo && resLogoId != -1) {
                Drawable dw = Constant.getContext().getResources().getDrawable(resLogoId);
                dw.setBounds(0, DRAWABLE_BOUNDS_TOP, DRAWABLE_BOUNDS_RIGHT, DRAWABLE_BOUNDS_BOTTOM);
                buttonText.setCompoundDrawables(dw, null, null, null);
            } else {
                buttonText.setCompoundDrawables(null, null, null, null);
            }

        } catch (Throwable e) {
            SmartSmsSdkUtil.smartSdkExceptionLog("BubbleBottomTwo setButtonTextAndImg error:", e);
        }

    }

    View.OnClickListener mOnClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            processOnClick((JSONObject) v.getTag());

        }

    };

    public void setBotton(View button, final TextView buttonText, final JSONObject actionMap, boolean disLogo) {
        if (actionMap == null) {
            return;
        }

        final String action = (String) JsonUtil.getValueFromJsonObject(actionMap, "action");
        String btnName = (String) JsonUtil.getValueFromJsonObject(actionMap, "btn_name");
        if (!StringUtils.isNull(btnName)) {
            buttonText.setText(btnName);
            setButtonTextAndImg(buttonText, action, disLogo);
        }
        if (!StringUtils.isNull(action)) {
            button.setTag(actionMap);
            button.setOnClickListener(mOnClickListener);
        }
    }

    public void setBotton(final Activity context, View button, final TextView buttonText, final JSONArray jsonArray,
            boolean disLogo) throws Exception {
        if (jsonArray == null || jsonArray.length() <= 0) {
            return;
        }
        final JSONObject actionMap = jsonArray.getJSONObject(0);
        final String action = (String) JsonUtil.getValueFromJsonObject(actionMap, "action");
        if (StringUtils.isNull(action)) {
            return;
        }
        // String btnName = (String) JsonUtil.getValueFromJsonObject(actionMap,
        // "btn_name");

        boolean isAddRemind = false;
        try{
            isAddRemind = (Boolean) mMessage.extendParamMap.get("has_added_calendar");
        }catch(Throwable ex){
            SmartSmsSdkUtil.smartSdkExceptionLog("BubbleBottomTwo", ex);
        }
        /* SMARTISAN-392 zhaoxiachao 20170221 start */
        String btnName = (String) ChannelContentUtil.getBtnName(actionMap, "egName", isAddRemind);
        /* SMARTISAN-392 zhaoxiachao 20170221 end */
        if (!StringUtils.isNull(btnName)) {
            buttonText.setText(btnName);
            setButtonTextAndImg(buttonText, action, disLogo);
        }

        if (!StringUtils.isNull(action)) {
            button.setTag(actionMap);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    /* SMARTISAN-378 zhaoxiachao 20161114 start */
                    Boolean clickAble = (Boolean) mMessage.getExtendParamValue(ContentUtil.ISCLICKABLE);
                    if (clickAble != null && clickAble.equals(Boolean.FALSE)) {
                        return;
                    }
                    /* SMARTISAN-378 zhaoxiachao 20161114 end */
                    int length = jsonArray.length();

                    if (length <= 1) {
                        processOnClick(actionMap);
                        return;
                    }

                    final MoreButtonDataDialog selectDialog = new MoreButtonDataDialog(jsonArray, mContext, 0);
                    selectDialog.ShowDialog(new OnBottomClick() {
                        @Override
                        public void Onclick(int type, int select) {
                            try {
                                if (type == MoreButtonDataDialog.CONFIRM) {
                                    processOnClick(jsonArray.getJSONObject(select));
                                }
                            } catch (Throwable e) {
                                SmartSmsSdkUtil.smartSdkExceptionLog("BubbleBottomTwo", e);
                            }
                        }
                    });
                }
            });
        }
    }

    private void processOnClick(JSONObject jsonObject) {
        HashMap<String, String> mValueMap = new HashMap<String, String>();
        try {
            if (mValueMap == null) {
                mValueMap = new HashMap<String, String>();
            } else {
                mValueMap.clear();
            }
            String action_data = (String) JsonUtil.getValueFromJsonObject(jsonObject, "action_data");
            mValueMap.put("ManufactureData", jsonObject.optString("ManufactureData", "XIAOYUAN"));
            SmsProcessFacade.getInstance().processAction(mContext, action_data, mValueMap);
        } catch (Throwable e) {
            SmartSmsSdkUtil.smartSdkExceptionLog("BubbleBottomTwo OnClickListener error:", e);
        }
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void setContent(BusinessSmsMessage message, boolean isRebind) throws Exception {
        this.mMessage = message;
        String textColor = (String) message.getValue("v_bt_color");
        ThemeUtil.setTextColor(mContext, mTextView1, textColor, R.color.duoqu_theme_color_3011);
        ThemeUtil.setTextColor(mContext, mTextView2, textColor, R.color.duoqu_theme_color_3011);
        ThemeUtil.setTextColor(mContext, mTextView3, textColor, R.color.duoqu_theme_color_3011);

        setBottom(message, isRebind);
    }

    private void setButtonViewVisibility(int button1Visible, int button2Visible, int button3Visible) {
        ChannelContentUtil.setViewVisibility(mBtn1, button1Visible);
        ChannelContentUtil.setViewVisibility(mBtn2, button2Visible);
        ChannelContentUtil.setViewVisibility(mBtn3, button3Visible);
        ChannelContentUtil.setViewVisibility(mTextView1, button1Visible);
        ChannelContentUtil.setViewVisibility(mTextView2, button2Visible);
        ChannelContentUtil.setViewVisibility(mTextView3, button3Visible);
    }

    @Override
    public void destroy() {
        super.destroy();
    }

    private void setBottom(BusinessSmsMessage message, boolean isRebind) {
        String groupValue = (String) mMessage.getValue("GROUP_KEY");
        if (StringUtils.isNull(groupValue)) {
            groupValue = "";
        }

        try {
            /* UIX标准方案UIX-167/2016.05.31/ kedeyuan starts */
            HashMap<String, Object> formatBubbleParamMap = message.extendParamMap;
            // Log.v("kedeyuan", message.bubbleJsonObj.toString());
            formatBubbleParamMap.put("isUseNewAction", "true");
            JSONArray actionArr = BottomButtonUtil.getAdAction(message, groupValue, 3, formatBubbleParamMap);
            List<JSONArray> arrays = ContentUtil.getSimpleBubbleData(actionArr, 3);
            /* UIX标准方案UIX-167/2016.05.31/ kedeyuan starts */
            if (arrays != null) {
                if(!StringUtils.isNull(mMessage.getValue(ChannelContentUtil.BUBBLE_SIZE)+"")){
                    for(JSONArray array : arrays){
                        for(int i=0;i<array.length();i++){
                            if("SDK_TIME_REMIND".equalsIgnoreCase(array.optJSONObject(i).optString("action"))){
                                arrays.remove(array);
                                break;
                            }
                        }
                    }
                }
                mSize = arrays.size();
            } else {
                mSize = 0;
            }
            if (mSize == 0) {
                mdlayout.setBackgroundResource(R.drawable.duoqu_v_by_bg_2);
                mdlayout.setPadding(0, 0, 0, 0);
                mBtnLay.setBackgroundResource(0);
            } else {
                mdlayout.setBackgroundResource(0);
                mdlayout.setPadding((int) mContext.getResources().getDimension(R.dimen.duoqu_bottom_two_margin_start), 0, (int) mContext.getResources().getDimension(R.dimen.duoqu_bottom_two_margin_end), 0);
                mBtnLay.setBackgroundResource(R.drawable.duoqu_v_bt_bg_1);
            }
            Boolean clickAble = (Boolean) mMessage.getExtendParamValue(ContentUtil.ISCLICKABLE);
            switch (mSize) {
            case 0:
                setButtonViewVisibility(View.GONE, View.GONE, View.GONE);
                break;
            case 1:
                setButtonViewVisibility(View.VISIBLE, View.GONE, View.GONE);
                setBotton(mContext, mBtn1, mTextView1, arrays.get(0), mDisLogo);
                break;
            case 2:
                setButtonViewVisibility(View.VISIBLE, View.VISIBLE, View.GONE);
                setBotton(mContext, mBtn1, mTextView1, arrays.get(1), mDisLogo);
                setBotton(mContext, mBtn2, mTextView2, arrays.get(0), mDisLogo);
                break;
            default:
                setButtonViewVisibility(View.VISIBLE, View.VISIBLE, View.VISIBLE);
                setBotton(mContext, mBtn3, mTextView3, arrays.get(2), mDisLogo);
                setBotton(mContext, mBtn1, mTextView1, arrays.get(1), mDisLogo);
                setBotton(mContext, mBtn2, mTextView2, arrays.get(0), mDisLogo);
                break;
            }
        } catch (Throwable e) {
            SmartSmsSdkUtil.smartSdkExceptionLog("setBottom exception: " + e.getMessage(), e);
        }
    }

    public void changeData(Map<String, Object> param) {
        try {
            String groupValue = "";
            if (param != null) {
                groupValue = (String) param.get("groupValue");
            }
            if (!StringUtils.isNull(groupValue)) {
                setBottom(this.mMessage, true);
            }
        } catch (Throwable e) {
            SmartSmsSdkUtil.smartSdkExceptionLog("BubbleBottomTwo changeData error:", e);
        }

    }
}
